from pydantic_settings import BaseSettings
from pydantic import Field
from pydantic import ConfigDict  # 引入配置字典类


class Settings(BaseSettings):
    API_V1_STR: str = "/api/v1"

    # MySQL 配置（用 alias 指定环境变量名，替代原来的 env）
    DB_USER: str = Field("root", alias="DB_USER")  # 从环境变量 DB_USER 读取，默认 root
    DB_PASSWORD: str = Field("123456", alias="DB_PASSWORD")  # 从环境变量 DB_PASSWORD 读取
    DB_HOST: str = Field("localhost", alias="DB_HOST")
    DB_PORT: str = Field("3306", alias="DB_PORT")
    DB_NAME: str = Field("myfastapi", alias="DB_NAME")

    # 拼接 MySQL 连接 URL
    @property
    def DB_URL(self) -> str:
        return f"mysql+pymysql://{self.DB_USER}:{self.DB_PASSWORD}@{self.DB_HOST}:{self.DB_PORT}/{self.DB_NAME}?charset=utf8mb4"

    # Pydantic v2 中用 model_config 替代 Config 子类
    model_config = ConfigDict(
        # 从 .env 文件读取环境变量
        # 环境变量不区分大小写（可选，根据需求调整）
    )


settings = Settings()